package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.SfaConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.SfaBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.SfaFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * sfa 消息队列
 * @author 魏荣杰
 * @date 2019/2/25 14:33
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(SfaConsumerBinder.class)
public class SfaListener {
    private static Logger logger = LoggerFactory.getLogger(SfaListener.class);

    @Resource
    private SfaFeignClient sfaFeignClient;

    @StreamListener(value = SfaBinderConstant.SFA_CUSTOMER_INPUT)
    public void customerReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case SFA_CUSTOMER_PUSH:
                    response = sfaFeignClient.customer(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SfaListener.customerReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SfaListener.customerReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = SfaBinderConstant.SFA_OPPORTUNITY_INPUT)
    public void opportunityReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case SFA_OPPORTUNITY_PUSH:
                    response = sfaFeignClient.opportunity(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SfaListener.opportunityReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SfaListener.opportunityReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = SfaBinderConstant.SFA_CONTRACT_INPUT)
    public void contractReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case SFA_CONTRACT_PUSH:
                    response = sfaFeignClient.contract(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SfaListener.contractReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SfaListener.contractReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = SfaBinderConstant.SFA_CONTACT_INPUT)
    public void contactReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case SFA_CONTACT_PUSH:
                    response = sfaFeignClient.contact(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SfaListener.contactReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SfaListener.contactReceive Exception, type=" + type, e);
        }
    }
}
